package main

import (
	"fmt"

	"gopkg.in/mgo.v2/bson"

	"gitee.com/Flearning/go_crawl/mgobson"
	"gitee.com/Flearning/go_crawl/xueqiu/userinfo"
)

var mgoXueqiuCollection string = "crawl_xueqiu_user"

func main() {
	var result []int
	mgobson.NewInit()
	c := mgobson.MgoSession.DB(mgobson.AuthDatabase).C(mgoXueqiuCollection)
	err := c.Find(nil).Distinct("id", &result)
	if err != nil {
		panic(err)
	}
	sigma := len(result)
	fmt.Println("Σ = ", sigma)
	for _, val := range result {
		clearUser(val)
		sigma--
		fmt.Println("Σ = ", sigma)
	}
}

func clearUser(uid int) {
	c := mgobson.MgoSession.DB(mgobson.AuthDatabase).C(mgoXueqiuCollection)
	us := new(userinfo.UserStruct)
	w := bson.M{
		"id": uid,
	}
	c.Find(w).Sort("-add_time").Limit(1).One(us)
	fmt.Println("user_id: ", uid)
	removeW := bson.M{
		"id":       us.Id,
		"add_time": bson.M{"$lt": us.AddTime},
	}
	info, _ := c.RemoveAll(removeW)
	fmt.Println("removed: ", info.Removed)
}
